/**
 *
 * @author cheng sir
 * 2016年9月3日下午4:16:22
 */
package com.angel.web.health;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.Constants;
import com.angel.core.base.JsonEntity;
import com.angel.core.base.PageData;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.DateUtils;
import com.angel.core.util.JsonResult;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.JsonUtils;
import com.angel.core.util.Pager;
import com.angel.core.util.SMSUtils;
import com.angel.health.BuzCustomer;
import com.angel.health.BuzCustomerLog;
import com.angel.health.BuzPackages;
import com.angel.health.BuzReserve;
import com.angel.health.BuzServicePackages;
import com.angel.health.BuzVideo;
import com.angel.health.SMS;
import com.angel.provider.health.BuzCustomerLogProvider;
import com.angel.provider.health.BuzCustomerProvider;
import com.angel.provider.health.BuzPackagesProvider;
import com.angel.provider.health.BuzReserveProvider;
import com.angel.provider.health.BuzServicePackagesProvider;
import com.angel.provider.health.BuzVideoProvider;

/**
 * @author cheng sir 2016年9月3日下午4:16:22
 */
@Controller
@RequestMapping("reserve")
public class BuzReserveController {

	@Autowired
	private BuzReserveProvider buzReserveService;
	@Autowired
	private BuzVideoProvider bvProvider;
	@Autowired
	private BuzPackagesProvider buzPackagesProvider;
	@Autowired
	private BuzServicePackagesProvider buzServicePackagesProvider;
	@Autowired
	private BuzCustomerLogProvider bclProvider;
	@Autowired
	private BuzCustomerProvider bcProvider;

	@RequestMapping("index")
	public String index(HttpServletRequest request, @ModelAttribute("buzReserve") BuzReserve buzReserve, Pager pager) {
		buzReserve.setEnable(1);
		pager = buzReserveService.findAll(buzReserve, pager);
		request.setAttribute("re", buzReserve.getSearchVal());
		request.setAttribute("data", pager);
		return "member/vip_server";
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/del")
	public String del(String ids) {
		try {
			String[] idlist = ids.split(",");
			if (idlist != null) {
				BuzReserve b = null;
				for (int i = 0; i < idlist.length; i++) {
					b = new BuzReserve();
					b.setId(Integer.parseInt(idlist[i]));
					buzReserveService.delById(b);
				}
			}
			return JsonUtil.toJson(new JsonEntity("200", JsonUtil.toJson(0)));
		} catch (Exception e) {
			return JsonUtil.toJson(new JsonEntity("-1", "修改失败"));
		}
	}

	/**
	 * 跳转添加页面
	 * 
	 * @return
	 */
	@RequestMapping("/toAdd")
	public String toAdd() {
		return "member/vip_server_add";
	}

	/**
	 * 跳转修改/查看页面
	 * 
	 * @return
	 */
	@RequestMapping("/toLook/{id}/{k}")
	public String toLook(HttpServletRequest request, @PathVariable("id") Integer id, @PathVariable("k") String k) {
		BuzReserve f = buzReserveService.lookById(id);
		request.setAttribute("re", f);
		request.setAttribute("k", k);
		if("3".equals(k)){
			return "member/vip_server_audit";
		}
		return "member/vip_server_look";
	}

	/**
	 * 添加服务
	 * @param buzReserve
	 * @param request
	 * @return
	 */
	@RequestMapping("addReserve")
	public String addReserve(@ModelAttribute("buzReserve") BuzReserve buzReserve,HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("user");
		buzReserve.setCreateBy(username);
		buzReserve.setUpdateBy(username);
		int save = buzReserveService.insert(buzReserve);
		
		if(save > 0){
			//新增预约日志
			BuzCustomerLog bcl = new BuzCustomerLog();
			if("1".equals(buzReserve.getPackageType())){
				bcl.setContent("您预约服务套餐成功！");
				bcl.setType(2);
			}else{
				bcl.setContent("您预约体检套餐成功！");
				bcl.setType(3);
			}
			bcl.setCustomerId(buzReserve.getCustomerId());
			bclProvider.insert(bcl);
			
			buzReserve.setCode(CodeUtil.createCode(null, Constants.CUSTOMERSERVICENUM, buzReserve.getId()));
			buzReserveService.updateById(buzReserve);
			
			if("1".equals(buzReserve.getPayStatus())){
				//发送短信
				BuzCustomer buzCustomer = new BuzCustomer();
				buzCustomer.setId(buzReserve.getCustomerId());
				buzCustomer = bcProvider.findById(buzCustomer);
				String single = Constants.SEND_SMS_SINGLE;
				String content = "尊敬的用户，您于"+DateUtils.getNowTimeForStr()+"预约套餐已成功！【天使健康】";
				SMSUtils.send(
						new SMS(buzCustomer.getId().toString(), buzCustomer.getMemPhone(), content, buzCustomer.getId().toString(), ""),
						single);
				if("1".equals(buzReserve.getPackageType())){
					//服务套餐添加音视频时间  
					BuzServicePackages bsp = new BuzServicePackages();
					bsp.setId(buzReserve.getServicePackageId());
					String videoTime = buzServicePackagesProvider.findById(bsp).getVideoTime();
					BuzVideo bv = new BuzVideo();
					int _videoTime = 0;
					try{
					_videoTime = Integer.parseInt(videoTime);
					}catch(Exception e){
					}
					bv.setVideoMinutes(_videoTime);
					bv.setCustomerId(buzReserve.getCustomerId());
					bv.setCreateTime(new Date());
					bvProvider.insert(bv);
				}
			}
		}
		
		return "redirect:/reserve/index";
	}

	/**
	 * 修改
	 * @param buzReserve
	 * @param request
	 * @return
	 */
	@RequestMapping("editReserve")
	public String editReserve(@ModelAttribute("buzReserve") BuzReserve buzReserve,HttpServletRequest request) {
//		BuzReserve br = buzReserveService.findById(buzReserve);
		String username = (String) request.getSession().getAttribute("user");
		buzReserve.setCreateBy(username);
		buzReserve.setUpdateBy(username);
		int update = buzReserveService.updateById(buzReserve);
		if(update > 0){
			buzReserve = buzReserveService.findById(buzReserve);
			if("1".equals(buzReserve.getPayStatus())){
				BuzCustomer buzCustomer = new BuzCustomer();
				buzCustomer.setId(buzReserve.getCustomerId());
				buzCustomer = bcProvider.findById(buzCustomer);
				String single = Constants.SEND_SMS_SINGLE;
				String content = "尊敬的用户，您于"+DateUtils.getNowTimeForStr()+"预约套餐已成功！【天使健康】";
				SMSUtils.send(
						new SMS(buzCustomer.getId().toString(), buzCustomer.getMemPhone(), content, buzCustomer.getId().toString(), ""),
						single);
				if("1".equals(buzReserve.getPackageType())){
					BuzServicePackages bsp = new BuzServicePackages();
					bsp.setId(buzReserve.getServicePackageId());
					String videoTime = buzServicePackagesProvider.findById(bsp).getVideoTime();
					BuzVideo bv = new BuzVideo();
					int _videoTime = 0;
					try{
					_videoTime = Integer.parseInt(videoTime);
					}catch(Exception e){
					}
					bv.setVideoMinutes(_videoTime);
					bv.setCustomerId(buzReserve.getCustomerId());
					bv.setCreateTime(new Date());
					bvProvider.insert(bv);
				}
			}
		}
		
		return "redirect:/reserve/index";
	}

	
	/**
	 * ajaxj异步load预约数据加载
	 * @param response
	 * @param request
	 * @param pager
	 */
	@RequestMapping("/ajaxLoadBuzPackages")
	public void ajaxLoadBuzPackages(HttpServletResponse response, HttpServletRequest request, Pager pager) {
		BuzPackages b = new BuzPackages();
		b.setEnable(1);
		Pager bean = buzPackagesProvider.findAll(b, pager);
		List<BuzPackages> list = new ArrayList<BuzPackages>();
		for (int i = 0; i < bean.getList().size(); i++) {
			list.add(JsonUtils.toObject(JsonUtils.toJson(bean.getList().get(i)), BuzPackages.class));
		}
		PageData<BuzPackages> pageData = new PageData<BuzPackages>(list, Integer.parseInt(bean.getTotal().toString()),
				bean.getCurrentPage(), bean.getPageSize());
		HashMap ajaxList = new HashMap();
		HashMap map = new HashMap();
		map.put("aaData", list);
		map.put("sEcho", request.getParameter("sEcho"));
		map.put("iTotalRecords", Integer.valueOf(pageData.getPageCount()));
		map.put("iTotalDisplayRecords", Integer.valueOf(pageData.getTotalCount()));
		ajaxList.put("returnObject", map);
		JsonResult.sendResult(response, JsonUtils.toJson(ajaxList));
	}

	
	/**
	 * ajaxj异步load体检数据加载
	 * @param response
	 * @param request
	 * @param pager
	 */
	@RequestMapping("/ajaxLoadBuzServicePackages")
	public void ajaxLoadBuzServicePackages(HttpServletResponse response, HttpServletRequest request, Pager pager) {
		BuzServicePackages b = new BuzServicePackages();
		b.setEnable(1);
		Pager bean = buzServicePackagesProvider.findAll(b, pager);
		 List<Map<String, Object>> findAll = buzServicePackagesProvider.findAll(b);
		List<BuzServicePackages> list = new ArrayList<BuzServicePackages>();
		for (int i = 0; i < bean.getList().size(); i++) {
			list.add(JsonUtils.toObject(JsonUtils.toJson(bean.getList().get(i)), BuzServicePackages.class));
		}
		PageData<BuzServicePackages> pageData = new PageData<>(list, Integer.parseInt(bean.getTotal().toString()),
				bean.getCurrentPage(), bean.getPageSize());
		HashMap ajaxList = new HashMap();
		HashMap map = new HashMap();
		map.put("aaData", list);
		map.put("sEcho", request.getParameter("sEcho"));
		map.put("iTotalRecords", Integer.valueOf(pageData.getPageCount()));
		map.put("iTotalDisplayRecords", Integer.valueOf(pageData.getTotalCount()));
		ajaxList.put("returnObject", map);
		JsonResult.sendResult(response, JsonUtils.toJson(ajaxList));
	}
	
	
}
